package com.yudaxian.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yudaxian.common.HoneyLogs;
import com.yudaxian.common.enums.LogType;
import com.yudaxian.common.vo.Result;
import com.yudaxian.sys.entity.ModelPipe;
import com.yudaxian.sys.service.IModelPipeService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yudaxian
 * @since 2023-12-07
 */
@RestController
@RequestMapping("/modelPipe")
public class ModelPipeController {


    @Resource
    private IModelPipeService ModelPipeService;

    @GetMapping("/all")
    public Result<List<ModelPipe>> getAllModelPipes() {
        List<ModelPipe> modelPipes = ModelPipeService.list();
        return Result.success(modelPipes);
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> getModelPipeList(
            @RequestParam(value = "pipeName", required = false) String pipeName,
            @RequestParam(value = "pageNo") Integer pageNo,
            @RequestParam(value = "pageSize") Integer pageSize) {

        LambdaQueryWrapper<ModelPipe> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(pipeName), ModelPipe::getPipeName, pipeName);

        Page<ModelPipe> page = new Page<>(pageNo, pageSize);
        ModelPipeService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    @HoneyLogs(operation = "模型管道", type = LogType.ADD)
    @PostMapping("/add")
    //RequestBody，json转ModelPipe对象，前端输入数据，传给后台json，再转成对象，存入数据库
    public Result<?> addModelPipe(@RequestBody ModelPipe modelPipe) {
        ModelPipeService.addModelPipe(modelPipe);
        return Result.success("新增传感器成功");
    }

    @HoneyLogs(operation = "模型管道", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result<?> updateModelPipe(@RequestBody ModelPipe modelPipe) {
        ModelPipeService.updateModelPipe(modelPipe);
        return Result.success("修改成功");
    }

    @GetMapping("{id}")
    public Result<ModelPipe> getModelPipeById(@PathVariable("id") Integer id) {
        ModelPipe modelPipe = ModelPipeService.getModelPipeById(id);

        return Result.success(modelPipe);
    }

    @HoneyLogs(operation = "模型管道", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteModelPipeById(@PathVariable("id") Integer id) {
        ModelPipeService.deleteModelPipeById(id);

        return Result.success("删除成功");
    }

}
